package com.dz.controller.manager;

import com.dz.annotation.OperationLog;
import com.dz.bean.Guide;
import com.dz.service.GuideService;
import com.dz.util.Page;
import com.dz.util.PasswordUtil;
import com.dz.util.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@CrossOrigin//允许跨域访问
@RequestMapping("/m/guide")
public class GuideManagerController {

    @Autowired
    GuideService guideService;

    @GetMapping("/page")
    public Result page(Page page, Guide guide){
        //分页查询
        //1. 查询总记录数
        //int totalCount = guideService.all(guide).size();
        //2. 查询当前页数据
        //int start = (page.getCurrentPage()-1)*page.getPageSize();
        //3. 组装结果
        //page.setTotalCount(totalCount);
        //4. 返回结果
        //return Result.success(page);
        guideService.page(page,guide);
        //System.out.println("---------"+guide);
        return Result.success(page);
    }

    @PostMapping
    @OperationLog("添加地陪")
    public Result add(@RequestBody Guide guide){
        // 设置默认密码并加密
        String defaultPassword = "123456"; // 默认密码
        guide.setPassword(PasswordUtil.getEncryption(defaultPassword));

        Object obj = guideService.add(guide);
        return Result.success(obj);
    }

    @DeleteMapping("/{id}")
    @OperationLog("删除地陪")
    public Result delete(@PathVariable("id") Integer id){
        Object obj = guideService.delete(id);
        return Result.success(obj);
    }

    @PutMapping
    @OperationLog("修改地陪信息")
    public Result edit(@RequestBody Guide guide){
        Object obj = guideService.edit(guide);
        return Result.success(obj);
    }

    @GetMapping("all")
    public Result all(Guide guide){
        Object obj = guideService.all(guide);
        return Result.success(obj);
    }

    @GetMapping("{id}")
    public Result findById(@PathVariable("id") Integer id){
        Object obj = guideService.findById(id);
        return Result.success(obj);
    }
}
